#include "qdevicedatawindow.h"
#include "ui_qdevicedatawindow.h"

#include "qprojectinformationtreewidget.h"
#include "qdevicefanctabwidget.h"
#include "qdevicedataview.h"
#include "qdriverlisternwidget.h"
#include "qhistorydatabasewidget.h"
#include "qlogview.h"

#include "../../libs/core/styledbar.h"
#include "../../libs/core/minisplitter.h"
#include "../../libs/core/qsoftcore.h"
#include "../../libs/device/qdevicemanager.h"

#include <QDesktopWidget>
#include <QApplication>
#include <QVBoxLayout>
#include <QLabel>
#include <QMessageBox>

QDeviceDataWindow::QDeviceDataWindow(tagDeviceInfo *info, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::QDeviceDataWindow),
    m_menu_bar(new QMenuBar(this)),
    m_status_bar(new QStatusBar),
    m_tree_widget(new QProjectInformationTreeWidget),
    m_device_info(*info),
    m_device_widget(new QDeviceFancTabWidget),
    m_data_view(new QDeviceDataView(info))
{
    this->setAttribute(Qt::WA_DeleteOnClose);
    ui->setupUi(this);
    this->setWindowTitle(tr("Device Data")+"--"+info->m_project_name+"--"+info->m_uuid);
    QDesktopWidget *desk=qApp->desktop();
    int w=desk->width();
    int h=desk->height();

    this->setMenuBar(m_menu_bar);
    this->setStatusBar(m_status_bar);
    this->setGeometry(w*0.1,h*0.1,w*0.8,h*0.8);

    //this->showMaximized();

    init_memu_bar();


    QWidget* wid=new QWidget;

    StyledBar *bar=new StyledBar;

    QLabel *label=new QLabel(tr("Device Property"));
    label->setStyleSheet("color: rgb(255,255,255);");

    QVBoxLayout *la=new QVBoxLayout;
    la->setContentsMargins(5,0,5,0);
    la->addWidget(label);
    bar->setLayout(la);

    QVBoxLayout *v=new QVBoxLayout;
    v->setMargin(0);
    v->setSpacing(0);
    v->addWidget(bar);
    v->addWidget(m_tree_widget);

    wid->setLayout(v);

    MiniSplitter *s=new MiniSplitter;
    s->addWidget(m_device_widget);
    s->addWidget(wid);
    s->setStretchFactor(0,1);
    s->setStretchFactor(1,0);
    setCentralWidget(s);

    setVisible(false);

    m_tree_widget->set_information(m_device_info.m_propertys);

    connect(QSoftCore::get_core()->get_device_manager(),SIGNAL(device_property_refreach(QString,QStringList)),
            this,SLOT(property_refresh(QString,QStringList)));
    connect(QSoftCore::get_core()->get_device_manager(),SIGNAL(device_offline(QString)),
            this,SLOT(device_offline(QString)));

    setProperty("panelwidget",true);

    m_status_bar->setSizeGripEnabled(false);

    wid=new QWidget();

    StyledBar *b=new StyledBar;
    v=new QVBoxLayout;
    v->setMargin(0);
    v->setSpacing(0);
    v->addWidget(b);
    v->addWidget(m_data_view);
    wid->setLayout(v);

    m_device_widget->insertTab(0,wid,QIcon(":/images/data.png"),tr("Data"));
    m_device_widget->insertTab(1,new QDriverListernWidget(info),
                               QIcon(":/images/driver.png"),tr("Driver"));
    m_device_widget->insertTab(2,new QHistoryDatabaseWidget(info),
                               QIcon(":/images/device_database.png"),tr("History"));
    m_device_widget->insertTab(3,new QLogView(info),
                               QIcon(":/images/log.png"),tr("Log"));

    m_device_widget->setCurrentIndex(0);
}

QDeviceDataWindow::~QDeviceDataWindow()
{
    delete ui;
}

void QDeviceDataWindow::init_memu_bar()
{
    QMenu *menu=m_menu_bar->addMenu(tr("File"));
    menu=m_menu_bar->addMenu(tr("Operator"));
}

void QDeviceDataWindow::property_refresh(const QString &uuid, const QStringList &list)
{
    if(uuid==m_device_info.m_uuid)
    {
        m_device_info.m_propertys=list;
        m_tree_widget->set_information(list);
    }
}

void QDeviceDataWindow::device_offline(const QString &uuid)
{
    if(uuid==m_device_info.m_uuid)
    {
        QMessageBox::critical(this,tr("Device Offline"),tr("The device is offline!"));
        this->close();
    }
}
